package com.itheima.backend.service;

import com.itheima.backend.mapper.UserMapper;
import com.itheima.backend.pojo.User;
import com.itheima.backend.service.JIEKOU.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> getAllUsers() {
        return userMapper.getAllUsers();
    }

    @Override
    public User getUserById(Integer id) {
        return userMapper.getUserById(id);
    }

    @Override
    public void createUser(User user) {
        userMapper.insertUser(user);
    }

    @Override
    public void deleteUserById(Integer id) {
        userMapper.deleteUserById(id);
    }
    public User login(String username, String password) {
        // 根据用户名查询用户
        User user = userMapper.findByUsername(username);

        if (user != null && user.getPassword().equals(password)) {
            // 验证通过
            return user;
        }
        // 验证失败
        return null;
    }

    @Override
    public Integer getUserIdByUsername(String username) {
        return userMapper.getUserIdByUsername(username);
    }



    //    @Override
//    public User login(String username, String password) {
//        // 根据用户名查询用户信息
//        User user = userMapper.getUserByUsername(username);
//
//        if (user == null) {
//            return null; // 用户不存在
//        }
//
//        // 使用 BCrypt 验证密码是否正确
//        if (BCrypt.checkpw(password, user.getPassword())) {
//            return user;
//        }
//
//        return null; // 密码错误
//    }

}
